package com.example.module_motorcircle.detail.viewmodel

import androidx.lifecycle.viewModelScope
import com.example.lib_common.base.BaseViewModel
import com.example.lib_common.state.UIState
import com.example.lib_net.model.api.AfterSalePageItem
import com.example.lib_net.model.api.PostCommentItem
import com.example.lib_net.model.api.PostItem
import com.example.module_motorcircle.detail.model.PostCommentRequestModel
import com.example.module_motorcircle.detail.repository.PostDetailRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.zip
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/14
 */
@HiltViewModel
class PostDetailViewModel @Inject constructor(private val repository: PostDetailRepository):BaseViewModel() {
    //当前帖子详情数据
    var currentPost:PostItem? = null
    //当前售后信息数据
    var afterSaleInfo:List<AfterSalePageItem>? = null
    //当前帖子评论数据
    var commentist:MutableList<PostCommentItem> = mutableListOf()


    private val initState = MutableStateFlow<UIState>(UIState.Default)
    val initState_ = initState.asStateFlow()



    /**
     * 请求帖子详情数据/售后信息/评论信息
     */
    fun loadPostDetailData(postId:String){
        viewModelScope.launch {
            //详情
            repository.postDetail(postId)
                .onStart {
                    initState.value = UIState.Loading
                }
                .catch {
                    initState.value = UIState.Error(it.message.toString())
                }
                .collectLatest {
                    if(it.code == 0 && it.data != null){
                        currentPost = it.data
                    }
                    //售后信息
                    repository.postAfterSale(postId)
                        .zip(
                            //评论信息
                            repository.postComment(postId,1,10)
                        ){afterInfo,commentInfo ->
                            if(afterInfo.code == 0 && afterInfo.data != null){
                                afterSaleInfo = afterInfo.data.records
                            }
                            if(commentInfo.code == 0 && commentInfo.data != null){
                                commentist.clear()
                               formatCommentList(commentInfo.data.records)
                            }
                        }.collectLatest {
                            initState.value = UIState.Success
                        }
                }
        }
    }

    /**
     * 格式化评论信息有二级评论
     */
    private fun formatCommentList(list:MutableList<PostCommentItem>){
        list.forEach{it->
            //一级评论
            it.isFirstLevel = true
            commentist?.add(it)
            //二级评论：多个
            if(it.twoLevel?.isNotEmpty() == true){
                it.twoLevel?.forEach {it->
                    it.isFirstLevel = false
                }
                commentist?.addAll(it.twoLevel!!)
            }
        }

    }
    //发布评论
    val postCommentModel:PostCommentRequestModel = PostCommentRequestModel()
    /**
     * 评论验证
     */
    private val commentValidateState = MutableStateFlow<UIState>(UIState.Default)
    val commentValidateState_ = commentValidateState.asStateFlow()
    fun commentValidate(postId:String,beReplyId:String,onPass:()->Unit){
        postCommentModel.postsId = postId
        postCommentModel.beReplyId = beReplyId
        execute(
            action = repository.postCommentValidation(postId,beReplyId),
            onStart = {
                commentValidateState.value = UIState.Loading
            },
            onError = {
                commentValidateState.value = UIState.Error(it)
            },
            onFinished = {
                commentValidateState.value = UIState.Success
                onPass.invoke()
            }

        )
    }
    /**
     * 发布评论
     */
    private val commentPostState = MutableStateFlow<UIState>(UIState.Default)
    val commentPostState_ = commentPostState.asStateFlow()
    fun postCommentSave(content:String){
        postCommentModel.content = content
        execute(
            action = repository.postCommentSave(postCommentModel),
            onStart = {
                commentPostState.value = UIState.Loading
            },
            onError = {
                commentPostState.value = UIState.Error(it)
            },
            onFinished = {
                commentPostState.value = UIState.Success
            }
        )
    }
}